<?php

//include_once('../includes/chromePhp.php');

function __autoload($class_name) {
    //class directories
    $directorys = array(
        '../classes/',
        '../includes/',
        '../libs/',
        '../config/'
    );

    //for each directory
    foreach ($directorys as $directory) {
        //see if the file exsists
        if (file_exists($directory . $class_name . '.php')) {
            require_once($directory . $class_name . '.php');
            //only require the class once, so quit after to save effort (if you got more, then name them something else 
            return;
        }
    }
}

$language = "../languages/en.php";

if (!file_exists($language)) {
    $language = "../languages/en.php";
}
// language file
require_once($language);

//require_once '../libs/ihelper.php';

/**
 * Application config.
 */
final class Config {

    const CONFIG_FILE = '../config/config.ini';

    /** @var array config data */
    private static $data = null;

    /** @var application name */
    private static $appname;

    /**
     * @return array
     * @throws Exception
     */
    public static function getConfig($section = null) {
        if ($section === null) {
            return self::getData();
        }
        try {
            $data = self::getData();
        } catch (Exception $e) {
            echo $e->getMessage();
        }

        if ($data && !array_key_exists($section, $data)) {
            throw new Exception('Unknown config section: ' . $section);
        }
        return $data[$section];
    }

    /**
     * @return array
     */
    private static function getData() {
        if (self::$data !== null) {
            return self::$data;
        }
        if (!file_exists(self::CONFIG_FILE)) {
            throw new Exception(self::CONFIG_FILE . ' file does not exist');
        } else {
            self::$data = parse_ini_file(self::CONFIG_FILE, true);
            return self::$data;
        }
    }

    public function getConfigItem($section, $item) {
        $config = Config::getConfig($section);
        $retval = $config[$item];
        
        return $retval;
    }
    
    public function getAppname() {
        if (empty(self::$appname)) {
            //ChromePhp::log(__METHOD__);
            $config = Config::getConfig("app");         // app section
            self::$appname = $config['appname'];

            if (self::$appname === "")
                self::$appname = '(not set)';
        }

        return self::$appname;
    }

    public function getCopyright() {
        if (empty(self::$copyright)) {
            //ChromePhp::log(__METHOD__);
            $config = Config::getConfig("app");         // app section
            $copyright = htmlentities($config['copyright']);

            if (empty($copyright))
                $copyright = '(copyright not set)';
        }
        return $copyright;
    }

}

?>
